package com.sensor.work.consumer;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sensor.work.entity.PudongDeviceConfig;
import com.sensor.work.entity.PudongDeviceStatus;
import com.sensor.work.service.IPudongDeviceConfigService;
import com.sensor.work.service.IPudongDeviceStatusService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.Map;

/**
 * 设备在线离线消费服务
 */
@Component
@RabbitListener(queues  ={"deviceStatus"})
@AllArgsConstructor
@Slf4j
public class DeviceStatusConsumer {

    @Autowired
    private IPudongDeviceConfigService deviceService;

    @Autowired
    private IPudongDeviceStatusService deviceStatusService;



    @RabbitHandler
    public void process(Map<String,Object> message) {
        log.info("Received <{}>", message);
        //
        Object deviceId = message.get("deviceId");
        //天翼 1在线 0离线
        String eventType = message.get("eventType").toString();


        long timestamp = (long) message.get("timestamp");


        UpdateWrapper<PudongDeviceConfig> objectUpdateWrapper = new UpdateWrapper<>();
        objectUpdateWrapper.eq("device_no",deviceId);
        objectUpdateWrapper.set("status",eventType);
        deviceService.update(objectUpdateWrapper);

        //设备状态表

        PudongDeviceStatus pudongDeviceStatus = new PudongDeviceStatus();
        pudongDeviceStatus.setDeviceNo((String) deviceId);
        log.info("设备号：{}",deviceId);
        if ("1".equals(eventType)){
//            System.out.println("11111111111111111111111111111111");
//            log.info("上线时间：{}",timestamp);
            pudongDeviceStatus.setDeviceOnlineTime(new Date(timestamp));

        }
        if ("0".equals(eventType)){
//            System.out.println("2222222222222222222222222");
//            log.info("离线时间：{}",timestamp);
            pudongDeviceStatus.setDeviceOfflineTime(new Date(timestamp));

        }
        saveOrUpdateByDeviceNo(pudongDeviceStatus,eventType);

    }


    private void saveOrUpdateByDeviceNo(PudongDeviceStatus pudongDeviceStatus ,String eventType){

        log.info("实体类新增:{}",pudongDeviceStatus);

        QueryWrapper<PudongDeviceStatus> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("device_no",pudongDeviceStatus.getDeviceNo());
        PudongDeviceStatus deviceStatus = deviceStatusService.getOne(queryWrapper);
        if (deviceStatus == null){
            deviceStatusService.save(pudongDeviceStatus);
            log.info("实体类新增:{}",pudongDeviceStatus);
        }

        else {
            UpdateWrapper<PudongDeviceStatus> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("device_no",pudongDeviceStatus.getDeviceNo());
            updateWrapper.set("device_status",eventType);
            //离线信息
            if ("0".equals(eventType)){
                log.info("离线时间：{}");

                Integer offlineSum = deviceStatus.getOfflineSum();
                if (offlineSum ==null){
                    offlineSum =0;
                }
                updateWrapper.set("offline_sum", offlineSum +1);
                updateWrapper.set("device_offline_time",pudongDeviceStatus.getDeviceOfflineTime());
                updateWrapper.set("device_status","0");
            }
            //在线信息
            if ("1".equals(eventType)){

                updateWrapper.set("device_online_time",pudongDeviceStatus.getDeviceOnlineTime());
                if (deviceStatus.getDeviceOfflineTime()!=null){
                    long online_time = pudongDeviceStatus.getDeviceOnlineTime().getTime();
                    long offline_time = deviceStatus.getDeviceOfflineTime().getTime();
                  long time =  online_time- offline_time;

                updateWrapper.set("offline_time",time/1000);
                log.info("离线时长：{}",time/1000);
                }


            }
            deviceStatusService.update(updateWrapper);

        }
    }







}
